/**
 * Created by cjh1 on 2016/8/23.
 */
$(document).ready(function () {

    var cptCondition = {
        level: '',
        type: ''
    };
    $('#cpt-list').hide();
    $('#cpt-page').hide();

    $('#condition-level-list').selectable({
        item: 'li',
        selected: function (e, item) {
            var $item = $(item);

            cptCondition.level = $item.data('value');
            $('.cpn-btn .confirm').on('click',function(){
                cptAjax();
                if(cptCondition.level == ''){
                    $('.header-selected2').text('-不限');
                }
                else{
                    $('.header-selected2').text('-'+cptCondition.level);
                }
                $('.cpn-header-type').attr('src','/uploads/demo/pull_down.png');
                $('.cpn-header-title2').css('color','#959595');
                $('.dialog-shade').fadeOut(200);
                $('.cpn-select').slideUp(200);
        });
        }
    });

   /* $('#condition-level-list').on('click', 'a', function (e) {
        e.preventDefault();

        var $this = $(this);
        $this.parent().addClass('active').siblings().removeClass('active');

        cptCondition.level = $this.data('value');
        cptAjax();
    });*/

    $('#condition-type-list').selectable({
        item: 'li',
        selected: function (e, item) {
            var $item = $(item);

            cptCondition.type = $item.data('value');
            $('.cpn-btn .confirm').on('click',function(){
                cptAjax();
                if(cptCondition.type==''){
                    $('.header-selected').text('-不限');
                }
                else{
                    $('.header-selected').text('-'+cptCondition.type);
                }
                $('.cpn-header-area').attr('src','/uploads/demo/pull_down.png');
                $('.cpn-header-title1').css('color','#959595');
                $('.dialog-shade').fadeOut(200);
                $('.cpn-select').slideUp(200);
            });

        }
    });
$('.cpn-header-area').on('click',function(){
        $('.cpn-header-area').attr('src','/uploads/demo/pull_up.png');
        $('.cpn-type').hide();
        $('.dialog-shade').css('display','block');
        $('.cpn-header-title1').css('color','#298aff');

        $('.cpn-select').fadeIn(300);
        $('.cpn-area').fadeIn(300);
    });
    $('.cpn-header-type').on('click',function(){
        $('.cpn-header-type').attr('src','/uploads/demo/pull_up.png');
        $('.cpn-area').hide();
        $('.dialog-shade').css('display','block');
        $('.cpn-header-title2').css('color','#298aff');
        $('.cpn-select').fadeIn(300);
        $('.cpn-type').fadeIn(300);
    });
    $('.cpn-select .cancel').on('click',function(){
        $('cpn-header-type').attr('src','/uploads/demo/pull_down.png');
        $('.cpn-header-title1').css('color','#959595');
        $('.cpn-header-title2').css('color','#959595');
        $('.dialog-shade').fadeOut(200);
        $('.cpn-select').slideUp(200);
        $('.cpn-header-area').attr('src','/uploads/demo/pull_down.png');
        $('.cpn-header-type').attr('src','/uploads/demo/pull_down.png');

    });
    /*$('#condition-type-list').on('click', 'a', function (e) {
        e.preventDefault();

        var $this = $(this);
        $this.parent().addClass('active').siblings().removeClass('active');

        cptCondition.type = $this.data('value');
        cptAjax();
    });*/

    // 竞赛结果
    var hash = window.location.hash;
    if (hash === '#result') {
        $('#my-tab li:eq(1) a').tab('show');
    }

    cptAjax(1, true);
    resultAjax(1, true);
    //userAjax(1, false);
    //companyAjax(1, false);

    function resultAjax(page, updatePage) {
        $.ajax({
            url: '/competition/resultAjax',
            data: {
                page: page,
                pageSize: 10
            },
            dataType: 'json',
            success: function (obj) {
                var list = obj.list;
                resultView(list);
                if (updatePage) {
                    resultPage(obj);
                }
            },
            error: function () {
                eui.msg('系统出错');
            }
        });
    }

    // 过滤标签保存纯文本TODO 需要重构
    function filterHtml(description) {
        var dd=description.replace(/<\/?.+?>/g,"");
        dd = dd.replace(/&lt;\/?.+?&gt;/g,"");
        dd = dd.replace(/&nbsp;/g, '').replace(/&lt;/g, '').replace(/&gt;/g, '')
            .replace(/&amp;/g, '').replace(/lt;/g, '').replace(/pgt;/g, '');
        return dd;
    }

    function simpleResult(result) {
        var filt = filterHtml(result);
        var LEN = 94;
        if (filt.length > LEN) {
            filt = filt.substring(0, LEN) + '...';
        }
        return filt;
        //replace(/<\/?.+?>/g,"");
    }

    function resultView(list) {
        if (!list.length) {
            $('#result-empty').show();
            $('#load-more-result').hide();
            $('#cpt-result-list').hide();
            return;
        }

        $('#result-empty').hide();
        if (list.page !== list.maxPage) {
            $('#load-more-result').show();
        }
        $('#cpt-result-list').show();

        var $ul = $('#cpt-result-list');
        var html = '';
        for (var i = 0; i < list.length; i++) {
            var result = list[i];
            html += '<li class="list-item">'
                + '<img class="item-logo col-xs-4" src="' + result.logo + '">'
                + '<div class="item-info">'
                + '<a class="item-side-name-result" href="/competition/result?id=' + result.cpt_id + '" >' + result.title + '</a>'
                + '<div class="cpt-info">'
                + '<div>'
                + '    <span class="info-name">主办方:</span>'
                + '    <span class="info-value">' + result.school.replace(/天生我才/, '平台') + '</span></div>'
                + '</div>'
                + '   <div class="item-time">'
                + '       <span class="info-name">时间:</span>'
                + '        <span class="info-value">' + result.start_time.substring(0, 10).replace(/-/g, '.') + ' - ' + result.end_time.substring(0, 10).replace(/-/g, '.') + '</span>'
                + '</div>'
                + '<div class="cpt-result line1"><span>' + simpleResult(result.cpt_result) + '</span></div>'
                + '</div>'
                + '</li>';

        }
        $ul.empty();
        $ul[0].innerHTML = html;
    }

    function resultPage(obj) {
        $('#load-more-result').loadmore({
            page: parseInt(obj.page),
            totalPage: obj.maxPage,
            onPage: function (page) {
                resultAjax(page);
            }
        });
    }

    function cptAjax(page, updatePage) {
        var load = eui.load(2, {
            scrollbar: true
        });
        $('#load-more-cpt').hide();
        $.ajax({
            url: '/competition/listAjax',
            data: {
                page: page,
                pageSize: 10,
                level: cptCondition.level,
                content_type: cptCondition.type
            },
            dataType: 'json',
            success: function (obj) {
                eui.close(load);
                var list = obj.list;
                cptView(list);
                if (updatePage) {
                    cptPage(obj);
                }
            },
            error: function () {
                eui.close(load);
                eui.msg('系统出错');
            }
        });
    }

    function cptView(list) {
        if (list.length === 0) {
            $('#search-empty').show();
            $('#cpt-list').hide();
            $('#load-more-cpt').hide();
            return;
        }

        $('#cpt-list').show();
        if (list.page !== list.maxPage) {
            $('#load-more-cpt').show();
        }

        $('#search-empty').hide();

        var $ul = $('#cpt-list');
        var html = '';
        for (var i = 0; i < list.length; i++) {
            var cpt = list[i];
            html += '<li class="list-item">'
                + '<img class="item-logo" src="' + cpt.logo + '">'
                + '<div class="item-info">'
                + '<a class="item-title" href="/competition/detail?id=' + cpt.cpt_id + '" >' + cpt.title + '</a>'
                + '<div class="cpt-info">'
                + '<div>'
                + '    <span class="info-name">主办方:</span>'
                + '    <span class="info-value">' + cpt.school.replace(/天生我才/, '平台') + '</span>'
                + '</div>'
                + '<div>'
                + '   <span class="info-name">时间:</span>'
                + '    <span class="info-value">' + cpt.start_time.substring(0, 10).replace(/-/g, '.') + ' - ' + cpt.end_time.substring(0, 10).replace(/-/g, '.') + '</span>'
                + '</div>'
                + '</div>'
                + '</div>'
                + function () {
                    var start = new Date(cpt.start_time);
                    var end = new Date(cpt.end_time);
                    var now = new Date();
                    if (now > start && now < end) {
                        return '<span class="cpt-state active">【正在报名】</span>';
                    } else if (now > end) {
                        return '<span class="cpt-state">【已结束】</span>';
                    } else {
                        return '<span class="cpt-state">【未开始】</span>';
                    }
                }()
                + '</li>';

        }
        $ul.empty();
        $ul[0].innerHTML = html;
    }

    function cptPage(obj) {
        $('#load-more-cpt').loadmore({
            page: parseInt(obj.page),
            totalPage: obj.maxPage,
            onPage: function (page) {
                cptAjax(page);
            }
        });
    }

    function userAjax(page, updatePage) {
        $.ajax({
            url: '/user/relev',
            data: {
                page: page,
                pageSize: 2
            },
            dataType: 'json',
            success: function (obj) {
                var list = obj.list;
                userView(list);
                if (updatePage) {
                    userPage(obj);
                }
            },
            error: function () {
                eui.msg('系统出错');
            }
        });
    }

    function userPage(obj) {
        $('#relative-user-page').page({
            page: parseInt(obj.page),
            pageSize: 2,
            count: 5,
            nextPage: true,
            totalPage: obj.maxPage,
            onPage: function (page) {
                userAjax(page);
            }
        });
    }

    function userView(list) {
        var $ul = $('#relative-user');
        var html = '';
        for (var i = 0; i < list.length; i++) {
            var user = list[i];
            html += '<li class="list-item">'
                + '<img class="item-logo" src="' + user.photo + '">'
                + '<div class="item-info">'
                + '<a class="item-title" href="/user/detail?id=' + user.id + '" >' + user.name + '</a>'
                + '<div class="item-profession">' + user.professional + '</div>'
                + '<div class="item-school">' + user.school + '</div>'
                + '</div></li>';

        }
        $ul.empty();
        $ul[0].innerHTML = html;
    }

});